package com.example.guoxun.livewallpagerdemo;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.service.wallpaper.WallpaperService;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.SurfaceHolder;

import java.io.IOException;

public class LiveWallpagerService extends WallpaperService {

    public static boolean hasCameraPermission = false;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d("LiveWallpagerService", "onCreate");
    }

    @Override
    public Engine onCreateEngine() {
        Log.d("LiveWallpagerService", "hasCameraPermission:" + hasCameraPermission);
        return new CameraEngine();
    }

    class CameraEngine extends Engine implements Camera.PreviewCallback {

        Camera mCamera;

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            if (ContextCompat.checkSelfPermission(LiveWallpagerService.this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(LiveWallpagerService.this, BlackActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                hasCameraPermission = false;
            } else {
                hasCameraPermission = true;
                startPreview();
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            System.out.println("onVisibilityChanged = " + visible);
            System.out.println("onVisibilityChanged = 是否有权限" + hasCameraPermission);
            if (visible) {
                startPreview();
            } else {
                stopPreview();
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();

            stopPreview();
        }

        private void startPreview() {
            if (!hasCameraPermission) return;

            mCamera = Camera.open();
            mCamera.setDisplayOrientation(90);

            try {
                mCamera.setPreviewDisplay(getSurfaceHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 构建一个相机的参数
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            mCamera.setParameters(parameters);
            mCamera.startPreview();
        }

        private void stopPreview() {
            if (mCamera != null) {
                try {
                    mCamera.stopPreview();
                    mCamera.setPreviewDisplay(null);
                    mCamera.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mCamera = null;
            }
        }

        @Override
        public void onPreviewFrame(byte[] bytes, Camera camera) {
            camera.addCallbackBuffer(bytes);
        }
    }

}
